﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Orid.Data.SqlServer;
using System.Data.SqlClient;

namespace Orid.Management.Statistics.Tools.Decomposition.Data
{
    class RoleLevel:DataBase
    {
        public virtual List<Model.ModelRoleLevel> GetRoleLevel()
        {
            return null;
        }


        protected List<Model.ModelRoleLevel> GetRoleLevel(string connectString, string tableName)
        {
            List<Model.ModelRoleLevel> modelRoleLevelList = new List<Model.ModelRoleLevel>();
            try
            {
                //[FutureManagement_Manage].[FutureMa].[RoleJobLevels]
                StringBuilder sqlText = new StringBuilder();
                sqlText.Append(" SELECT [RoleListID],[RoleID],[ParentID],[LevelName],[IsLastNode] ");
                sqlText.Append(" FROM  " + tableName + "  [RoleJobLevels] with(NOLOCK) ");
 
                using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectString))
                {
                    conn.Open();

                    SqlDataReader reader =  SqlHelper.ExecuteReader(conn, System.Data.CommandType.Text, sqlText.ToString());
                    while (reader.Read())
                    {
                        Model.ModelRoleLevel roleLevel = new Model.ModelRoleLevel();

                        if (reader["RoleListID"] != DBNull.Value)
                        {
                            roleLevel.RoleListID = Convert.ToInt32(reader["RoleListID"]);
                        }

                        if (reader["RoleID"] != DBNull.Value)
                        {
                            roleLevel.RoleID = Convert.ToInt32(reader["RoleID"]);
                        }

                        if (reader["ParentID"] != DBNull.Value)
                        {
                            roleLevel.ParentID = Convert.ToInt32(reader["ParentID"]);
                        }

                        if (reader["LevelName"] != DBNull.Value)
                        {
                            roleLevel.LevelName = Convert.ToString(reader["LevelName"]);
                        }

                        if (reader["IsLastNode"] != DBNull.Value)
                        {
                            roleLevel.IsLastNode = Convert.ToBoolean(reader["IsLastNode"]);
                        }

                        modelRoleLevelList.Add(roleLevel);
                    }

                    reader.Close();

                    if (conn.State == System.Data.ConnectionState.Open)
                        conn.Close();


                }
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                throw (new Exception(ex.Message));
            }

            return modelRoleLevelList;
        }

        
    }
}
